package com.gdcp.controller.portal;

import com.alibaba.fastjson.JSONObject;
import com.gdcp.beans.Category;
import com.gdcp.beans.Product;
import com.gdcp.beans.Shop;
import com.gdcp.beans.User;
import com.gdcp.config.RequestHolder;
import com.gdcp.config.annotation.CategoryByAll;
import com.gdcp.service.CategoryService;
import com.gdcp.service.ProductService;
import com.gdcp.service.ShopService;
import com.gdcp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/render/shop/")
public class ShopController {

    @Autowired
    ProductService productService;
    @Autowired
    ShopService shopService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    UserService userService;

    @RequestMapping(value = "/queryShopByShopId")
    @CategoryByAll(flush = true)
    public String queryShopByShopId(){
        HttpServletRequest request= RequestHolder.getRequest();
        Integer shop_id= Integer.valueOf(request.getParameter("shop_id"));
        List<Category> categories=categoryService.queryByShopId(shop_id);

        //根据shop_id和category_id查询商品
        List<Product> products=productService.queryShopProductByShopId(shop_id);

        //感觉shop_id查询商店信息
        Shop shop=shopService.queryShopInfoByShopId(shop_id);
        //获取商店里的shop_main_recommend,查询商品信息
        JSONObject jsonObj = JSONObject.parseObject(shop.getShop_special_recommend());

        Integer shop_special_recommend_one_id=null;
        Integer shop_special_recommend_two_id=null;
        Integer shop_main_recommend=null;
        if (shop.getShop_main_recommend()!=null){
            shop_main_recommend=Integer.valueOf(shop.getShop_main_recommend());
        }
        if (jsonObj!=null){
            if (jsonObj.getString("shop_special_recommend_one")!=null){
                shop_special_recommend_one_id= Integer.valueOf(jsonObj.getString("shop_special_recommend_one"));
            }
            if (jsonObj.getString("shop_special_recommend_two")!=null){
                shop_special_recommend_two_id= Integer.valueOf(jsonObj.getString("shop_special_recommend_two"));
            }
        }

        Product Shop_main_recommend=productService.queryProductByShopId(shop_main_recommend);
        Product shop_special_recommend_one=productService.queryProductByShopId(shop_special_recommend_one_id);
        //获取商店里的shop_special_recommend，json,查询商品信息
        Product shop_special_recommend_two=productService.queryProductByShopId(shop_special_recommend_two_id);

        User user=userService.getInformation(shop.getUser_id());

        request.setAttribute("shop_products",products);
        request.setAttribute("categories",categories);
        request.setAttribute("shop",shop);
        request.setAttribute("user",user);
        request.setAttribute("shop_main_recommend",Shop_main_recommend);
        request.setAttribute("shop_special_recommend_one",shop_special_recommend_one);
        request.setAttribute("shop_special_recommend_two",shop_special_recommend_two);

        int count=shopService.updatePopularity(shop.getId());
        if(count<=0){
            return "frontend/index";
        }
        return "frontend/shop";
    }
}
